﻿using System.Linq;
using System;
using System.Collections.Generic; 
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace A2PongGame
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class Console : DrawableGameComponent
    {
        Texture2D sprite;
        private int consoleYposition = -120;


        private List<Commands> commands;
        private Guid currentCommandID = Guid.Empty;
        private KeyboardState _previousKeyboardState;
        private KeyboardState _currentKeyboardState;
        
        public bool active { get; set; }



        public Console(Game game)
            : base(game)
        {
            sprite = new Texture2D(game.GraphicsDevice, 1, 1);
            sprite.SetData(new Color[] { Color.Red });
           
            commands = new List<Commands>();
            currentCommandID = Guid.NewGuid();
            commands.Add(new Commands(this.Game, this.currentCommandID, String.Empty, DateTime.UtcNow));

        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            // TODO: Add your initialization code here

            base.Initialize();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            _previousKeyboardState = _currentKeyboardState;
            _currentKeyboardState = Keyboard.GetState();



            if (active)
            {



                //if (_previousKeyboardState.IsKeyDown(Keys.A) && _currentKeyboardState.GetPressedKeys().Length == 0) commands.Find(r => r.ID == currentCommandID).Text += "a";
                //if (_previousKeyboardState.IsKeyDown(Keys.B) && _currentKeyboardState.GetPressedKeys().Length == 0) commands.Find(r => r.ID == currentCommandID).Text += "b";
               

                if (_previousKeyboardState.IsKeyDown(Keys.Enter) && _currentKeyboardState.GetPressedKeys().Length == 0)
                {
                    commands.Where(r => r.ID == currentCommandID).FirstOrDefault().Excute();
                    currentCommandID = Guid.NewGuid();
                    var tmpcmd = new Commands(this.Game ,currentCommandID, String.Empty, DateTime.UtcNow);
                    commands.Add(tmpcmd);
                }



                if (_previousKeyboardState.GetPressedKeys().Length == 1 && _currentKeyboardState.GetPressedKeys().Length == 0)
                {
                    var c = (char)(int)_previousKeyboardState.GetPressedKeys()[0];
                    if (c >= 65 && c <= 90)
                        commands.Where(r => r.ID == currentCommandID).FirstOrDefault().Text += c;

                }

                if (_previousKeyboardState.IsKeyDown(Keys.Back) && _currentKeyboardState.GetPressedKeys().Length == 0)
                {
                    int lenght = commands.Where(r => r.ID == currentCommandID).FirstOrDefault().Text.Length;
                    commands.Where(r => r.ID == currentCommandID).FirstOrDefault().Text = commands.Where(r => r.ID == currentCommandID).FirstOrDefault().Text.Remove(lenght - 1);
                }



            }


            if (!active && _currentKeyboardState.IsKeyDown(Keys.C))
            {
                consoleYposition = 0;
                active = true;
            }
            if (_currentKeyboardState.IsKeyDown(Keys.Escape))
            {
                consoleYposition = -120;
                active = false;
            }

            base.Update(gameTime);
        }
        public override void Draw(GameTime gameTime)
        {
            Game1.spriteBatch.Begin();

            Game1.spriteBatch.Draw(sprite, new Rectangle(0, consoleYposition, Game.Window.ClientBounds.Width, 120), new Color(0, 0, 0, 80));
            if (active)
            {
                int tempY = 100;
                foreach (var command in commands.OrderByDescending(r => r.Time))
                {
                    Game1.spriteBatch.DrawString(Game1.myfont, command.Text, new Vector2(0, tempY), Color.White);
                    tempY -= 15;
                }
            }

            Game1.spriteBatch.End();

            base.Draw(gameTime);
        }


    }

    public class Commands
    {
        private Game game;
        public string Text { get; set; }
        public Guid ID { get; set; }
        public DateTime Time { get; set; }

        public Commands(Game game , Guid id, string text, DateTime time)
        {
            this.ID = id;
            this.Text = text;
            this.Time = time;
            this.game = game;
        }

        public  void Excute()
        {
            if(Text == "EXIT")
            {
               game.Exit();
            }
            if(Text == "CCOLOR")
            {
                Game1.bgColor = Color.Black;

            }
        }

    }
}
